<?php

namespace App\Http\Controllers\back;


use App\Model\feedback;
use App\Model\readuser;
use App\Model\tofeedback;
use App\permission;
use App\role;
use App\role_user;
use App\User;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class BackController extends Controller
{
    //首页
    public function index()
    {
        return view('.back.index');
    }

    //权限管理
    public function power()
    {
        $permissions = DB::table('permissions')->paginate(4);
        return view('back.power', compact('permissions'));
    }

    //角色管理
    public function role()
    {
        $role = role::all();
        foreach ($role as $roles) {
            $perms = array();
            foreach ($roles->perms as $perm) {
                $perms[] = $perm->display_name;
            }
            $roles->perms = implode(',', $perms);
        }
        return view('back.role', compact('role'));
    }

    //管理员管理
    public function strator()
    {
        $data = user::all();

//        dd($data);
        foreach($data as $user)
        {
            $roles=array();
            foreach ($user->roles as $role){
                $roles[]=$role->display_name;
            }
            $user->rolles=implode(',', $roles);
        }
        return view('back.strator', compact('data'));
    }

    //增加权限
    public function addpower(Request $request)
    {
        if ($request->isMethod('post')) {
//            dd($request);
            $permission = new permission();
            $permission->name = $request->name;
            $permission->display_name = $request->display_name;
            $permission->description = $request->description;
            $permission->save();
            return redirect('/back/power');
        }
        return view('back.addpower');
    }


    public function adddd(Request $request){
        $permission = new permission();
        $permission->name = $request->name;
        $permission->display_name = $request->display_name;
        $permission->description = $request->description;
        $permission->save();
        return redirect('/back/power');
//        dd($request);
    }

    //修改权限
    public function updatapower(Request $request,$id)
    {
        if ($request->isMethod('post')){
            $up=permission::find($id);
            $up->name=$request->name;
            $up->display_name=$request->display_name;
            $up->description=$request->description;
            $up->save();
            return redirect('/back/power');
        }
        $new=permission::find($id);
        return view('back.updatapower',compact('new'));
    }

    //删除权限
    public function deletepower($id)
    {
        $del=permission::find($id);
        $del->delete();
//        DB::table('permission_role')->where('permission_id')->delete();
        return redirect('back/power');
    }

    //增加角色
    public function addrole(Request $request)
    {
        if ($request->isMethod('post')) {
            $role = new role();
            $role->name = $request->name;
            $role->display_name = $request->display_name;
            $role->description = $request->description;
            $role->save();
            return redirect('/back/role');
        }
        return view('back.addrole');
    }

    //分配权限表
    public function deal(Request $request, $idrole)
    {
//        dd($idrole);
        if ($request->isMethod('post')) {
            $role = role::find($idrole);

            $jg = DB::table('permission_role')->where('role_id', $idrole)->delete();
//            dd($request->input('permission_id'));
            foreach ($request->input('permission_id') as $permission_id) {
                $role->attachPermission(permission::find($permission_id));
            }
            return redirect('back/role');
        }
        dump($idrole);
        $perm = permission::all();
        return view('back.deal', compact('perm'));
    }

    //分配角色
    public function userdeal(Request $request, $iduser)
    {
        if ($request->isMethod('post')) {
//            $user=user::find($iduser);

            DB::table('role_user')->where('user_id', $iduser)->delete();
            //$del->delete();
            $user = User::find($iduser);
            foreach ($request->input('role_id') as $role_id) {
//                $add= new role_user();
//                $add->user_id=$iduser;
//                $add->role_id=$role_id;
//                $add->save();
                $user->attachRole(role::find($role_id));

            }

            return redirect('back/strator');
        }

        $roles = role::all();

        return view('back.userdeal', compact('roles'));
    }

    //角色删除表
    public function roledelete($idrole)
    {
        return view('back.roledelete');
    }

    //角色修改表
    public function roleupdata(Request $request,$id)
    {
        if ($request->isMethod('post')){
            $uprole=role::find($id);
            $uprole->name=$request->name;
            $uprole->display_name=$request->display_name;
            $uprole->description=$request->description;
            $uprole->save();
            return redirect('back/role');
        }
        $new=role::find($id);
        return view('back.updatarole',compact('new'));
    }

    //新增管理员
    public function addstrator(Request $request)
    {
        if ($request->isMethod('post')){
            $rules=array(
                'name'=>'required|between:6,10',
                'password'=>'required|between:6,12|confirmed',
                'email'=>'required|email|unique:readusers,email',
            );
            $mess=array(
                'name.required'=>'用户名不能为空',
                'name.between'=>'用户名不能为空',
                'password.required'=>'密码不能为空',
                'password.confirmed'=>'两次密码不一致',
                'email.unique'=>'邮箱已被注册',
                'email.required'=>'邮箱不能为空',
                'email.email'=>'邮箱格式不正确',
            );
            $this->validate($request,$rules,$mess);

            //获得的数据
            $email=$request->email;
            $name=$request->name;
            $password=$request->password;
            $oldpassword=$request->password_confirmation;

            $old=User::where('name',$name)->where('email',$email)->get();
            if (!count($old)){
                $newadd=new User();
                $newadd->name=$name;
                $newadd->email=$email;
                $newadd->password=$password;
                $newadd->save();

            }else{
                return redirect('back/addstrator')->with('addstrator','名字或邮箱以被注册');
            }

        }
        return view('back.addstrator');
    }

    //登入
    public function login(Request $request)
    {
        if ($request->isMethod('post')) {
            $reules = array(
                'name' => 'required',
                'password' => 'required',
                'code' => 'captcha|required',
            );
            $mess = array(
                'name.required' => '用户不能为空',
                'password.required' => '密码不能为空',
                'code.required' => '验证码不能为空',
                'code.captcha' => '验证码错误',
            );
            $this->validate($request, $reules, $mess);
            $name = $request->name;
            $pass = $request->password;
            $data = user::where('name', $name)->where('password', $pass)->get();
            foreach ($data as $k) {
                $id = $k->id;
            }
            if (count($data)) {

                session(['user' => $name, 'userid' => $id]);
                return redirect('/back/index');
            }else{
                $erro='1';
                return view('back.login',compact('erro'));
            }

        }
        return view('back.login');
    }

    //清空session
    public function setse(Request $request)
    {
        $request->session()->flush();
        return redirect('back/login');
    }

    //用户反馈
    public function feedback()
    {
//        $order=feedback::all();
        $order = DB::table('feedbacks')->paginate(3);
        return view('back.feedback',compact('order'));
    }

    //回复用户反馈
    public function infeedback(Request $request,$id)
    {
        if ($request->isMethod('post')){
            $add=new tofeedback();
            $add->addtime=time();
            $add->tonum=$request->tonum;
            $add->fid=$id;
            $c=$add->save();
            if ($c){
                $fe=feedback::find($id);
                $fe->in=1;
                $fe->save();
            }
            return redirect('back/feedback');
        }

        return view('back.tofeedback');
    }

    //查看反馈ajax
    public function gofe(Request $request)
    {
        $id=$request->id;
        $new=tofeedback::where('fid',$id)->get();
//        $num=$new->tonum;
        foreach ($new as $k){
            $cum=$k->tonum;
        }
        return $cum;
    }

    //删除反馈
    public function delfeed($id){
//        $a=feedback::find($id);
// //        $a->delete();
//             $new=tofeedback::where('fid',$id)->get();
            

//         if (!empty(count($new))){
//            dd($id);
//            $c=tofeedback::where('fid',$id)->get();
            $c=DB::table('feedbacks')->where('id',$id)->delete();
//            $c->delete();
        // }

        return redirect('back/feedback');
    }

}
